package com;

import com.javabean.Customer;
import com.service.CustomerService;
import com.util.MailUntil;
import com.util.MD5Example;
import com.util.UsercheckUtil;
import lombok.extern.slf4j.Slf4j;

import javax.mail.MessagingException;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Scanner;

class Main {
    static CustomerService customerService = new CustomerService();

    public static void main(String[] args) throws MessagingException {
        UsercheckUtil usercheckUtil = new UsercheckUtil();
        MailUntil mailUntil = new MailUntil();
        MD5Example md5Example = new MD5Example();
        Scanner scanner = new Scanner(System.in);


        System.out.println("------------------欢迎使用客户信息管理系统------------------");
        System.out.println("是否需要注册新账号:是/否");
        if (scanner.next().equals("是")) {
            handleRegistration(scanner, mailUntil, usercheckUtil, md5Example, customerService);
        }

        System.out.println("------------------欢迎登录客户信息管理系统------------------");
        handleLoginAndOperations(scanner, customerService, usercheckUtil, md5Example);
    }

    /*
     *
     * 处理用户注册的逻辑流程
     *
     * @param scanner         用于接收用户输入的Scanner对象
     * @param mailUntil       用于发送邮件验证码的工具类实例
     * @param userInputUtil   用于处理用户输入相关辅助操作的工具类实例
     * @param md5Example      用于对密码进行MD5加密的工具类实例
     * @param customerService 用于操作客户信息的服务类实例
     */


    private static void handleRegistration(Scanner scanner, MailUntil mailUntil, UsercheckUtil usercheckUtil,
                                           MD5Example md5Example, CustomerService customerService) throws MessagingException {
        System.out.print("欢迎注册客户信息管理系统,请输入您的qq邮箱：");
        String qqEmail = scanner.next();
        int sentEmailCode = mailUntil.sendemail(qqEmail);

        boolean isVerified = false;
        while (!isVerified) {
            System.out.print("请输入您收到的验证码(若重新发送请输入0)：");
            String inputCode = scanner.next();
            if ("0".equals(inputCode)) {
                sentEmailCode = mailUntil.sendemail(qqEmail);
//                handleRegistration(scanner, mailUntil, userInputUtil, md5Example, customerService);
                continue;

            }
            try {
                if (sentEmailCode == Integer.parseInt(inputCode)) {
                    isVerified = true;
                    Customer customer = buildCustomerFromInput(scanner, usercheckUtil, md5Example);
                    customerService.addCustomer(customer);
                } else {
                    System.out.println("请输入正确的验证码!");
                }
            } catch (NumberFormatException e) {
                System.out.println("输入的验证码格式不正确，请重新输入");
            }
        }
    }

    /*
     *
     * 根据用户输入构建一个Customer对象
     *
     * @param scanner         用于接收用户输入的Scanner对象
     * @param userInputUtil   用于处理用户输入相关辅助操作的工具类实例
     * @param md5Example      用于对密码进行MD5加密的工具类实例
     * @return 构建好的Customer对象
     */


    public static Customer buildCustomerFromInput(Scanner scanner, UsercheckUtil usercheckUtil, MD5Example md5Example) {
        CustomerService customerService1 = customerService;
        Customer customer = new Customer();
        System.out.print("姓名:");
        customer.setNAME(scanner.next());
        System.out.print("性别:");
        customer.setGender(scanner.next());
        System.out.print("年龄:");
        customer.setAge(usercheckUtil.inputCustomerage());
        System.out.print("工资:");
        customer.setSalary(scanner.nextDouble());
        customer.setPhone(usercheckUtil.inputCustomerphonenumber());


        boolean KEY = true;

        while (KEY) {
            System.out.print("用户账户:");
            String customers_user1 =UsercheckUtil.inputCustomeruer();

            Customer tempCustomer = UsercheckUtil.customers_user(customers_user1);
//            System.out.println(tempCustomer);
            if (tempCustomer==null) {
                KEY = false;
                customer.setUser(customers_user1);
            } else {
                System.out.println("用户账号已存在，请重新输入");
            }

        }
        /* while (KEY) {
             System.out.print("用户账户:");
             String customers_user1 = usercheckUtil.inputCustomeruer();
             System.out.println(customers_user1);
             Customer tempCustomer = usercheckUtil.custodmers_user(customers_user1);
             System.out.println(tempCustomer.getUser());
             if (tempCustomer!= null) {
                 customers_user2 = tempCustomer.getUser();
                 if (customers_user2.equals(customers_user1)) {
                     System.out.println("用户账号已存在，请重新输入");
                 } else {
                     KEY = false;
                     customer.setUser(customers_user1);
                 }
             }
         }*/

        System.out.print("密码:");
        String password = scanner.next();
        String MD5password = md5Example.md5(password);
        customer.setPassword(MD5password);


        return customer;
    }

  /*  public static Customer customers_user(String user) {
        List<Customer> customersList = customerService.getCustomers(user );
        if (customersList!= null &&!customersList.isEmpty()) {
            return customersList.get(0);
        }
        return null;
    }*/

    /*
     *
     * 处理用户登录及登录成功后的操作逻辑
     *
     * @param scanner         用于接收用户输入的Scanner对象
     * @param customerService 用于操作客户信息的服务类实例
     * @param userInputUtil   用于处理用户输入相关辅助操作的工具类实例
     * @param md5Example      用于对密码进行MD5加密的工具类实例

     */

    private static <customers> int handleLoginAndOperations(Scanner scanner, CustomerService customerService,
                                                            UsercheckUtil usercheckUtil, MD5Example md5Example) {
        System.out.print("用户账户:");
        String user = scanner.next();
        System.out.print("密码:");
        String password = scanner.next();
        String MD5password = md5Example.md5(password);
        customerService.getCustomers(user);
        for (Customer c : customerService.getCustomers(user)
        ) {
            if (Objects.equals(c.getUser(), user) && c.getPassword().equals(MD5password)) {
//                System.out.println(c.getUser()+c.getPassword());
                dhandleCustomerManagement(scanner, customerService, usercheckUtil);
            } else {
                System.out.println("登录失败,请重新登录");
                try {
                    main(null);
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
        }


       /* if (customerService.isValidUser(userAccount, MD5password)) {
            handleCustomerManagement(scanner, customerService, userInputUtil);
        } else {
            System.out.println("登录失败,请重新登录");
            try {
                main(null);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }*/
        return 0;
    }

    /*
     *
     * 处理登录成功后的客户信息管理操作，如添加、修改、删除客户等
     *
     * @param scanner         用于接收用户输入的Scanner对象
     * @param customerService 用于操作客户信息的服务类实例
     * @param userInputUtil   用于处理用户输入相关辅助操作的工具类实例

     */

    private static void dhandleCustomerManagement(Scanner scanner, CustomerService customerService,
                                                  UsercheckUtil usercheckUtil) {
        int option = 0;
        do {
            printCustomerList(customerService);
            System.out.print("1、添加客户 2、修改客户 3、删除客户 4、退出  请输入（1-4）");
            option = scanner.nextInt();
            switch (option) {
                case 1:
                    Customer newCustomer = buildCustomerFromInput(scanner, usercheckUtil, new MD5Example());
                    customerService.addCustomer(newCustomer);
                    break;
                case 2:
                    Customer updatedCustomer = buildUpdatedCustomerFromInput(scanner, usercheckUtil, new MD5Example());
                    if (customerService.updateCustomer(updatedCustomer)) {
                        System.out.println("修改成功");
                    } else {
                        System.out.println("修改失败");
                    }
                    break;
                case 3:
                    System.out.print("要删除的ID:");
                    int idToDelete = scanner.nextInt();
                    if (customerService.deleteCustomer(idToDelete)) {
                        System.out.println("删除成功");
                    } else {
                        System.out.println("删除失败");
                    }
                    break;
                case 4:
                    break;
                default:
                    System.out.println("----------------------------------------------");
                    System.out.println("非法输入");
            }
        } while (option != 4);
    }

    /**
     * 根据用户输入构建用于更新的Customer对象
     *
     * @param scanner       用于接收用户输入的Scanner对象
     * @param usercheckUtil 用于处理用户输入相关辅助操作的工具类实例
     * @param md5Example    用于对密码进行MD5加密的工具类实例
     * @return 构建好的用于更新的Customer对象
     */

    private static Customer buildUpdatedCustomerFromInput(Scanner scanner, UsercheckUtil usercheckUtil,
                                                          MD5Example md5Example) {
        Customer customer = new Customer();
        System.out.print("要修改的ID:");
        customer.setId(scanner.nextInt());
        System.out.print("姓名:");
        customer.setNAME(scanner.next());
        System.out.print("性别:");
        customer.setGender(scanner.next());
        System.out.print("年龄:");
        customer.setAge(usercheckUtil.inputCustomerage());
        System.out.print("工资:");
        customer.setSalary(scanner.nextDouble());
        customer.setPhone(usercheckUtil.inputCustomerphonenumber());
        System.out.print("账号:");
//        customer.setUser(usercheckUtil.inputCustomeruer());
        customer.setUser(scanner.next());
        System.out.print("密码:");
        String password = scanner.next();
        String MD5password = md5Example.md5(password);
        customer.setPassword(MD5password);
        return customer;
    }

    /*
     *
     * 打印客户列表信息
     *
     * @param customerService 用于操作客户信息的服务类实例
     */


    private static void printCustomerList(CustomerService customerService) {
        System.out.println("------------------客户信息管理------------------");
        System.out.println("--------------------客户列表--------------------");
        System.out.println("ID\t\t姓名\t\t性别\t\t年龄\t\t工资\t\t\t\t电话\t\t\t\t用户");
        for (Customer customer : customerService.getList()) {
            System.out.println(customer.getId() + "\t\t" + customer.getNAME() + "\t\t" + customer.getGender()
                    + "\t\t" + customer.getAge() + "\t\t" + customer.getSalary() + "\t\t" + customer.getPhone()
                    + "\t\t" + customer.getUser());
        }
        System.out.println("----------------------------------------------");
    }
}
